
import { Component, Emit, Prop } from 'vue-property-decorator';
import { BaseController, PageVO, DialogVO, YhRspType, YhResponseData, DateUtils } from '@yhfin/biz';
import InvestmentService from '../service/InvestmentService';
import { UploadFile } from '../model/UploadFile';
import VXETable from 'vxe-table'
import { downLoadFile } from '@/utils/request';
@Component({
    components: {},
    watch: {
    }
})
export default class InvestmentUploadController extends BaseController {
    private dialogVO: DialogVO = new DialogVO();
    private service: InvestmentService = new InvestmentService();
    fileList: [];
    uploadList: UploadFile[] = [];
    selectList: UploadFile[] = [];
    success = 0;
    fail = 0;
    private uploading = false;
    private uploaded = false;
    open() {
        this.uploaded = false;
        this.success = 0;
        this.fail = 0;
        this.selectList = [];
        this.uploadList = [];
        this.dialogVO = new DialogVO().getUpdateDialog('批量导入投监表');
        if (this.$refs.upload) {
            this.$refs.upload.clearFiles();
        }
    }

    selectionChange(table) {
        this.selectList = table.selection;
    }

    failMap: any = null

    submitUpload() {
        console.info(this.selectList);
        // 判断是否有文件再上传
        if (this.selectList.length === 0) {
            return this.$message.warning('请选取文件后再上传')
        }
        // 下面的代码将创建一个空的FormData对象:
        const formData = new FormData()
        // 你可以使用FormData.append来添加键/值对到表单里面；
        this.selectList.forEach((file) => {
            formData.append('file', file['raw'])
        })
        this.uploading = true;
        this.service.impoertExcel(formData).then((res: any) => {
            res.list.forEach(e => {
                this.uploadList.forEach(f => {
                    if (e.fileName == f.fileName) {
                        this.$set(f, 'remarks', e.remarks)
                        this.$set(f, 'status', e.status)
                    }
                })
            });
            this.success = this.uploadList.filter(e => e.status == '1').length;
            this.fail = this.uploadList.filter(e => e.status == '0').length;
            this.failMap = res.failMap
        }).catch(e => {
            console.info(e);
        }).finally(() => {
            this.uploading = false;
            this.uploaded = true;
        })
    }

    handleChange(file, fileList) {
        this.uploadList = []
        this.fileList = fileList;
        this.fileList.forEach(file => {
            const uploadFile = new UploadFile();
            uploadFile.fileName = file['name'];
            uploadFile.raw = file['raw']
            this.uploadList.push(uploadFile)
        })
        this.$nextTick(() => {
            this.$refs.refTable.setAllCheckboxRow(true)
            this.selectList = this.uploadList;
        })
    }

    clear() {
        if (this.uploaded) {
            this.uploaded = false;
        }
        this.$refs.upload.clearFiles();
    }

    cancle() {
        this.service.excelRollback().then(() => {
            this.dialogVO.visible = false;
            this.$emit('reload');
        })
    }

    comfirm_() {
        this.dialogVO.visible = false;
        this.service.confirm().then(() => {
            this.dialogVO.visible = false;
            this.$emit('reload');
        })
    }

    downFail() {
        console.log(this.failMap, 'failMap');
        if (this.failMap && JSON.stringify(this.failMap) !== '{}') {
            for (const key in this.failMap) {
                if (Object.prototype.hasOwnProperty.call(this.failMap, key)) {
                    const element = this.failMap[key];
                    const data = {
                        originalFilename: key,
                        list: element
                    };
                    downLoadFile({
                        method: 'post',
                        url: '/investMent/downloadFailFile',
                        data,
                        loading: true,
                        loadingMsg: '正在准备导出，请稍后··· ···'
                    })
                }
            }
        } else {
            this.yh_message_warn('暂无失败明细');
        }
    }
}
